package arithmetic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringArithmetic {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        String[] rows = new String[numRows];
        for (int i = 0; i < rows.length; i++) {
            rows[i] = "";
        }
        int index = 0;
        boolean check = true;
        for (int i = 0; i < s.length(); i++) {
            rows[index] += s.charAt(i);
            if (check) {
                if (index == rows.length - 1) {
                    check = !check;
                    index--;
                } else index++;
            } else if (index == 0) {
                check = !check;
                index++;
            } else index--;

        }
        String result = "";
        for (int i = 0; i < rows.length; i++) {
            result += rows[i].toString();
        }
        return result;
    }

    //最长公共前缀
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0 || strs[0].length() == 0 || strs == null || strs[0] == null)
            return "";
        StringBuffer sb = new StringBuffer();
        StringBuffer result = new StringBuffer();
        boolean check = true;
        for (int i = 0; i < strs[0].length(); i++) {
            sb.append(strs[0].charAt(i));
            for (int j = 1; j < strs.length; j++) {
                if (i > strs[j].length() || !strs[j].startsWith(sb.toString())) {
                    check = false;
                    break;
                }
            }
            if (check)
                result.append(strs[0].charAt(i));
            check = true;
        }
        return result.toString();
    }

    //38.报数
    public String countAndSay(int n) {
        if (n <= 0)
            return "";
        StringBuffer sb = new StringBuffer("1");
        for (int i = 1; i < n; i++) {
            int size = 1;
            StringBuffer sbTemp = new StringBuffer();
            for (int j = 1; j < sb.length(); j++) {
                if (sb.charAt(j) == sb.charAt(j - 1))
                    size++;
                else {
                    sbTemp.append(size + "" + sb.charAt(j - 1));
                    size = 1;
                }
            }
            sbTemp.append(size + "" + sb.charAt(sb.length() - 1));
            sb = sbTemp;
        }

        return sb.toString();
    }

    //    5. 最长回文子串 中心扩散
    public String longestPalindrome(String s) {
        if (s.length() == 1)
            return s;
        int startIndex = 0;
        int maxLength = 0;
        for (int i = 0; i < s.length(); i++) {
            int left = i - 1;
            int right = i + 1;
            while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
                if (right - left + 1 > maxLength) {
                    maxLength = right - left + 1;
                    startIndex = left;
                }
                right++;
                left--;
            }
        }
        for (int i = 0; i < s.length(); i++) {
            int left = i;
            int right = i + 1;
            while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
                if (right - left + 1 > maxLength) {
                    maxLength = right - left + 1;
                    startIndex = left;
                }
                right++;
                left--;
            }
        }
        return s.substring(startIndex, startIndex + maxLength);
    }

    //    8. 字符串转换整数 (atoi)
    public int myAtoi(String str) {
        if (str.trim().length() == 0)
            return 0;
        boolean abs = true;
        long result = 0;
        int index = 0;
        while (str.charAt(index) == ' ')
            index++;
        if (str.charAt(index) == '-') {
            abs = false;
            index++;
        } else if (str.charAt(index) == '+') {
            index++;
        }
        while (index < str.length() && str.charAt(index) >= '0' && str.charAt(index) <= '9') {
            result = result * 10 + (str.charAt(index) - '0');
            if (result > Integer.MAX_VALUE) {
                if (!abs) {
                    return Integer.MIN_VALUE;
                }
                return Integer.MAX_VALUE;
            }
            index++;
        }

        if (!abs)
            result = 0 - result;
        if (result > Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        if (result < Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        return (int) result;
    }

    //58最后一个单词的长度
    public int lengthOfLastWord(String s) {
        if (s.length() == 0 || s == null)
            return 0;
        StringBuffer sb = new StringBuffer();
        StringBuffer s1 = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                if (!s1.toString().equals("")) {
                    sb = s1;
                    s1 = new StringBuffer();
                }
                continue;
            }
            s1.append(s.charAt(i));
        }
        if (!s1.toString().equals(""))
            sb = s1;
        return sb.length();
    }
    public int singleNumber(int[] nums) {
        int result = nums[0];
        return Arrays.stream(nums).reduce(result,(i,j)->i^j);
    }
}
